package com.eatwhat.mapper;

import com.eatwhat.entity.Category;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface CategoryMapper {
    
    @Select("SELECT * FROM category ORDER BY sort_order ASC, id DESC")
    List<Category> findAll();
    
    @Select("SELECT * FROM category WHERE status = 1 ORDER BY sort_order ASC")
    List<Category> findAllEnabled();
    
    @Select("SELECT * FROM category WHERE id = #{id}")
    Category findById(Long id);
    
    @Insert("INSERT INTO category(name, description, sort_order, status) " +
            "VALUES(#{name}, #{description}, #{sortOrder}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Category category);
    
    @Update("UPDATE category SET name = #{name}, description = #{description}, " +
            "sort_order = #{sortOrder}, status = #{status} WHERE id = #{id}")
    int update(Category category);
    
    @Delete("DELETE FROM category WHERE id = #{id}")
    int deleteById(Long id);
    
    @Select("SELECT COUNT(*) FROM food WHERE category_id = #{categoryId}")
    int countFoodsByCategory(Long categoryId);
}